package com.qingxin.park.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import com.qingxin.park.entity.Company;
import com.qingxin.park.entity.FarmPark;
import com.qingxin.park.entity.WellProduct;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.ui.bean.TreeNode;

@Service
public class WellProductService extends BaseService<WellProduct, String>{
	
	/**
	 * 获取菜单树数据
	 * @param loginUser
	 * @param isParent
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TreeNode> getUITreeData(LoginUser loginUser){
		List<TreeNode> list = new ArrayList<TreeNode>();
		StringBuilder hql = new StringBuilder();
		hql.append("select new com.qingxin.park.entity.FarmPark(parkid,parkName)");
		hql.append(" from FarmPark");
		hql.append(" order by createTime asc");
		List<FarmPark> farmParks = listByHql(hql.toString());
		if(farmParks != null){
			for(FarmPark farmPark : farmParks){
				list.add(new TreeNode(farmPark.getParkid(),farmPark.getParkName(),"WELL",null,false));
			}
		}
		return list;
	}
	
	
	/**
	 * 获取指定数量的产品信息，用于首页展示
	 * @param nums
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<WellProduct> listWellProducts(int nums){
		Querier querier = Querier.create("select new com.qingxin.park.entity.WellProduct(" +
				"w.productid, w.productName, w.identificationTime, w.introduce, w.productImg, p.parkName)" +
				" from WellProduct w, FarmPark p where w.parkid = p.parkid order by w.createTime desc");
		querier.setMaxResults(nums);
		List<WellProduct> list = listByQuerier(querier);
		return list;
	}
	
	
	
	/**
	 * 获取指定园区下的产品信息
	 * @param parkid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<WellProduct> listParkWellProducts(String parkid){
		String hql = "from WellProduct w where w.parkid=? order by w.createTime desc";
		List<WellProduct> list = listByHql(hql, parkid);
		return list;
	}
	
	
	
	/**
	 * 通过类型获取分页数据
	 * @param categoryName
	 * @return
	 */
	public Pager list(PageParam pageParam,String type,HttpServletRequest request){
		Querier querier = Querier.create("select new com.qingxin.park.entity.WellProduct(" +
				"w.productid,w.parkid,w.companyid,w.productName,w.type,w.identification,w.identificationTime,w.productImg,p.parkName,'')" +
				" from WellProduct w, FarmPark p where w.parkid = p.parkid");
		if(!type.equals("6")){
			querier.append(" and w.type=:type ");
			querier.setParam("type", type);
		}
		querier.append(pageParam.getOrderby(" order by w.identificationTime desc"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	/**
	 * 库存数据
	 * @param pageParam
	 * @param search
	 * @param parkid
	 * @param request
	 * @return
	 */
	public Pager list(PageParam pageParam,String datatype,String search,String parkid, LoginUser loginUser, HttpServletRequest request){
		Querier querier = Querier.create("select new com.qingxin.park.entity.WellProduct(" +
				"w.productid,w.parkid,w.companyid,w.productName,w.type,p.parkName,c.companyName)" +
				"from WellProduct w, FarmPark p ,Company c where w.parkid = p.parkid and w.companyid = c.companyid");
		
		if(datatype.equals("GROUP")){
			String groupid = null;
			if(!SystemCfg.isAdminUser(loginUser) && !SystemCfg.isSystemAdminUser(loginUser)){
				groupid = loginUser.getGroupid();
			}
			
			if(StringUtils.isNotEmpty(groupid) && !"0".equals(groupid)){
				querier.append(" and w.parkid in (select distinct v.parkid from FarmPark v where v.groupid like '"+groupid+"%')");
			}
			
			if(!SystemCfg.isDataAdminUser(loginUser)){
				querier.append(" and w.parkid in (select distinct v.farmPark.parkid from SysUserParkRel v where v.sysUser.userid='"+loginUser.getUserid()+"')");
			}
			
		}else {
			querier.append(" and w.parkid=:parkid ");
			querier.setParam("parkid", parkid);
		}
		
		querier.append(" order by w.identificationTime desc");
		
		Pager  pager = pageByQuerier(querier, pageParam);
		return pager;
		
	}
	
	/**
	 * 添加
	 * @param wellProduct
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(WellProduct wellProduct,LoginUser loginUser, HttpServletRequest request){
		Date now = new Date();
		wellProduct.setCreateTime(now);
		wellProduct.setCreateBy(loginUser.getLogin_name());
		
		if(StringUtils.isNotEmpty(wellProduct.getProductImg())){
			UploadFile.moveTmpTo(wellProduct.getProductImg(),UploadFile.DIR_WELLPRODUCT_IMG);
		}
		if(StringUtils.isNotEmpty(wellProduct.getIdentification())){
			UploadFile.moveTmpTo(wellProduct.getIdentification(),UploadFile.DIR_WELLPRODUCT_IMG);
			wellProduct.setIdentificationTime(now);
		}
		this.save(wellProduct);	
		return null;
	}
	
	/**
	 * 删除
	 * @param productid
	 * @param request
	 * @return
	 */
	public SysError delete(String[] productid, HttpServletRequest request){
		for(String id : productid){
			WellProduct wellProduct = this.get(id);
			if(wellProduct == null){
				continue;
			}
			this.delete(wellProduct);
		}
		return null;
	}
	
	/**
	 * 修改
	 * @param wellProduct
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(WellProduct wellProduct, LoginUser loginUser, HttpServletRequest request){
		WellProduct old = getSafeEntity(wellProduct.getProductid());
		Date now = new Date();
		if(StringUtils.isNotEmpty(wellProduct.getProductImg()) && !wellProduct.getProductImg().equals(old.getProductImg())){
			UploadFile.moveTmpTo(wellProduct.getProductImg(),UploadFile.DIR_WELLPRODUCT_IMG);
		}
		if(StringUtils.isNotEmpty(wellProduct.getIdentification()) && !wellProduct.getIdentification().equals(old.getIdentification())){
			UploadFile.moveTmpTo(wellProduct.getIdentification(),UploadFile.DIR_WELLPRODUCT_IMG);
			wellProduct.setIdentificationTime(now);
		}
		String fields = "companyid,productName,type,identification,identificationTime,introduce,productImg";
		BeanUtil.setTargetFields(old, wellProduct, fields, true);
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(now);
		Updater<WellProduct> updater = new Updater<>(old, fields+",updateTime,updateBy");
		this.update(updater);
		return null;
	}
	/**
	 * 判断指定项目名称是否被使用
	 * @param itemName
	 * @param itemId
	 * @param taskId
	 * @return
	 */
	public boolean isNameDuplicate(String materialName, String repertoryid, String parkid){
		if(StringUtils.isEmpty(repertoryid)){
			Object obj = getByHql("select repertoryid from Repertory where materialName=? and parkid = ?", materialName,parkid);
			return obj!=null;
		}else {
			Object obj = getByHql("select repertoryid from Repertory where parkid=? and materialName=? and repertoryid<>?",parkid ,materialName, repertoryid);
			return obj!=null;
		}
	}
	
	/**
	 * 获得所有公司
	 * @param loginUser
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Company> getParkData(String parkid,LoginUser loginUser){
		List<Company> list = listByHql("select new com.qingxin.park.entity.Company(companyid,companyName)" +
				" from Company where companyid in (select new com.qingxin.park.entity.FarmPark(companyid)" +
				" from FarmPark where parkid='"+parkid+"' ) ");
		if(list == null){
			return new ArrayList<Company>();
		}
		return list;
	}
	
}
